package com.ujiuye.controller;


import com.ujiuye.config.MessageConstant;
import com.ujiuye.config.PageResult;
import com.ujiuye.config.QueryPageBean;
import com.ujiuye.config.Result;
import com.ujiuye.pojo.Checkitem;
import com.ujiuye.service.ICheckitemService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 检查项 前端控制器
 * </p>
 *
 * @author zs
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/checkitem")
public class CheckitemController {
    @Reference
    private ICheckitemService checkitemService;

    @PostMapping("list")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public PageResult list(@RequestBody QueryPageBean queryPageBean){
        try {
            return checkitemService.listPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result remove(@PathVariable("id") int id){
        try {
            checkitemService.removeById(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }

    }


    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result getById(@PathVariable("id") int id){
        try {
            Checkitem checkitem = checkitemService.getById(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL,null);
        }
    }
    @PutMapping("update")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result update(@RequestBody Checkitem checkitem){
        try {
            checkitemService.updateById(checkitem);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }

    }
    @PostMapping("save")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result save(@RequestBody Checkitem checkitem){
        try {
            checkitemService.save(checkitem);
            return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    @GetMapping
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findAll(){
        try {
            List<Checkitem> list = checkitemService.list();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL,null);
        }
    }







}

